home *** CD-ROM | disk | FTP | other *** search
- #include <Resources.h>
- #include <Sound.h>
- #include <SoundInput.h>
- #include <TextUtils.h>
-
- #include "ClockComponent.h"
- #include "MicroSeconds.h"
-
- /* Our version of a Sound Manager 3.1 */
- /* the one in Sound.h returns a nasty struct which is really a long */
- extern pascal long rSndSoundManagerVersion(void)
- FOURWORDINLINE(0x203c, 0x000c, 0x0008, 0xa800);
-
- pascal ComponentResult SoundClockComponent(ComponentParameters *tp,Handle s);
-
- typedef struct {
- unsigned long ticks;
- unsigned long lastMicroSeconds;
- SndChannelPtr channel;
- ExtSoundHeader header;
- unsigned short shortSampleRate;
- Component capturedClock;
- } SoundClockSharedGlobalsRecord, *SoundClockSharedGlobals;
-
- typedef struct {
- ComponentInstance delegateComponent;
- ComponentInstance self;
- SoundClockSharedGlobals sharedGlobals;
- } SoundClockGlobalsRecord, *SoundClockGlobals;
-
- #define DEFINE_CLOCK_COMPONENT
- #define cnames(a) SoundClock##a
- #define compID SoundClockGlobals store
- #define clockCallBackType void *
- #include "ClockComponent.h"
-
- #define kSoundClockVersion 0x00010004
-
- OSErr primeCallBack(SoundClockSharedGlobals sharedGlobals);
- pascal void soundClockCompletionRoutine(SndChannelPtr chan, SndCommand *cmd);
-
- #if !INITBUILD
- ComponentFunctionUPP SoundClockSelectorLookup( short selector );
- pascal ComponentResult SoundClockComponent( ComponentParameters *params, Handle storage );
-
- pascal ComponentResult SoundClockComponent( ComponentParameters *params, Handle storage )
- {
- ComponentFunctionUPP componentProc = 0;
- ComponentResult retstat = 0;
-
- componentProc = SoundClockSelectorLookup( params->what );
-
- if (componentProc)
- retstat = CallComponentFunctionWithStorage(storage, params, componentProc);
- else {
- retstat = DelegateComponentCall(params, **(ComponentInstance **)storage);
- }
- bail:
- return retstat; /* is this okay? */
- }
-
- ComponentFunctionUPP SoundClockSelectorLookup( short selector )
- {
- ComponentFunctionUPP componentProc = 0;
-
- #undef ComponentCall
- #define ComponentCall(a) case kClock##a##Selector: componentProc = (ComponentFunctionUPP)SoundClock##a; break;
- #define ComponentNoError(a) case kClock##a##Selector: componentProc = (ComponentFunctionUPP)-1; break;
- #define ComponentDelegate(a)
- #define ComponentError(a)
-
- switch (selector) {
- #include "SoundClockDispatch.h"
- }
-
- return componentProc;
- }
- #else
- #define SoundClockSelectorLookup COMPONENTSELECTORLOOKUP
- ComponentFunctionUPP SoundClockSelectorLookup( short selector );
- #endif
-
- pascal ComponentResult SoundClockCanDo(SoundClockGlobals store, short ftnNumber)
- {
- ComponentResult result;
-
- result = (SoundClockSelectorLookup( ftnNumber ) != 0);
- if (!result) {
- result = ClockCanDo( store->delegateComponent, ftnNumber );
- }
- return result;
- }
-
- pascal ComponentResult SoundClockVersion(SoundClockGlobals store)
- {
- #pragma unused(storage)
-
- return kSoundClockVersion;
- }
-
- pascal ComponentResult SoundClockOpen( SoundClockGlobals store, ComponentInstance self )
- {
- Component callBackClockComponent;
- ComponentResult err = noErr;
- SoundClockSharedGlobals sharedGlobals;
- THz saveZone = GetZone();
-
- if (rSndSoundManagerVersion() < 0x03100000)
- return paramErr;
-
- store = (SoundClockGlobals)NewPtrClear(sizeof(SoundClockGlobalsRecord));
- if (err = MemError()) goto bail;
-
- store->self = self;
- SetComponentInstanceStorage(self, (Handle)store);
-
- sharedGlobals = (SoundClockSharedGlobals)GetComponentRefcon((Component)self);
- if (!sharedGlobals) {
- sharedGlobals = (SoundClockSharedGlobals)NewPtrSysClear(sizeof(SoundClockSharedGlobalsRecord));
- if (err = MemError()) goto bail;
- SetComponentRefcon((Component)self, (long)sharedGlobals);
- }
- store->sharedGlobals = sharedGlobals;
-
- err = SoundClockRegister(store);
- if (err) goto bail;
-
- if (!sharedGlobals->channel) {
- SetZone(SystemZone());
-
- err = SndNewChannel(&sharedGlobals->channel, sampledSynth, 0, soundClockCompletionRoutine);
- if (err) goto bail;
-
- err = SndGetInfo(sharedGlobals->channel, siSampleRate, &sharedGlobals->header.sampleRate);
- if (err) goto bail;
-
- sharedGlobals->shortSampleRate = (unsigned short)(sharedGlobals->header.sampleRate >> 16);
-
- err = SndGetInfo(sharedGlobals->channel, siSampleSize, &sharedGlobals->header.sampleSize);
- if (err) goto bail;
-
- err = SndGetInfo(sharedGlobals->channel, siNumberChannels, &sharedGlobals->header.numChannels);
- if (err) goto bail;
-
- sharedGlobals->header.samplePtr = NewPtrSysClear(1024 * 2 * 2);
- // sharedGlobals->header.numChannels = 2;
- // sharedGlobals->header.sampleRate = rate44khz;
- sharedGlobals->header.loopStart = 0;
- sharedGlobals->header.loopEnd = 0;
- sharedGlobals->header.encode = extSH;
- sharedGlobals->header.baseFrequency = kMiddleC;
- sharedGlobals->header.numFrames = 1024;
- sharedGlobals->header.AIFFSampleRate = 0;
- sharedGlobals->header.markerChunk = nil;
- sharedGlobals->header.instrumentChunks = nil;
- sharedGlobals->header.AESRecording = nil;
- // sharedGlobals->header.sampleSize = 16;
- sharedGlobals->header.futureUse1 = 0;
- sharedGlobals->header.futureUse2 = 0;
- sharedGlobals->header.futureUse3 = 0;
- sharedGlobals->header.futureUse4 = 0;
-
- err = primeCallBack(sharedGlobals);
- if (err) goto bail;
- }
-
- store->delegateComponent = OpenComponent(store->sharedGlobals->capturedClock);
- if (!store->delegateComponent) {
- err = cantOpenHandler;
- goto bail;
- }
-
- bail:
- SetZone(saveZone);
-
- return err;
- }
-
- pascal ComponentResult SoundClockClose( SoundClockGlobals store, ComponentInstance self )
- {
- #pragma unused(self)
- if (store) {
- if (CountComponentInstances((Component)self) == 1) {
- SoundClockSharedGlobals sharedGlobals = store->sharedGlobals;
-
- if (sharedGlobals) {
- if (sharedGlobals->channel) {
- SndDisposeChannel(sharedGlobals->channel, true);
- sharedGlobals->channel = nil;
- }
- if (sharedGlobals->header.samplePtr) {
- DisposePtr(sharedGlobals->header.samplePtr);
- sharedGlobals->header.samplePtr = nil;
- }
- }
- }
- CloseComponent(store->delegateComponent);
- DisposePtr((Ptr)store);
- }
-
- return noErr;
- }
-
- pascal ComponentResult SoundClockRegister (SoundClockGlobals store)
- {
- ComponentDescription someComponent;
- Component callBackClockComponent;
- OSErr err;
-
- if (store->sharedGlobals == nil)
- return paramErr;
-
- if (store->sharedGlobals->capturedClock)
- return noErr;
-
- someComponent.componentType = clockComponentType;
- someComponent.componentSubType = 0;
- someComponent.componentManufacturer = 'appl';
- someComponent.componentFlags = kClockRateIsLinear | kClockImplementsCallBacks;
- someComponent.componentFlagsMask = someComponent.componentFlags;
- callBackClockComponent = FindNextComponent(0, &someComponent);
- if (!callBackClockComponent) {
- err = cantFindHandler;
- goto bail;
- }
-
- store->sharedGlobals->capturedClock = CaptureComponent(callBackClockComponent, (Component)store->self);
- if (store->sharedGlobals->capturedClock == nil)
- return cantOpenHandler;
-
- bail:
- return err;
- }
-
- pascal ComponentResult SoundClockUnregister (SoundClockGlobals store)
- {
- if (store->sharedGlobals == nil)
- return paramErr;
-
- if (store->sharedGlobals->capturedClock)
- UncaptureComponent(store->sharedGlobals->capturedClock);
-
- return noErr;
- }
-
- pascal ComponentResult SoundClockGetTime( SoundClockGlobals store, register TimeRecord *theTime)
- {
- #pragma unused(store)
-
- theTime->value.lo = UnsignedFixedMulDiv(store->sharedGlobals->ticks, 1000000, store->sharedGlobals->shortSampleRate);
- theTime->value.lo += (unsigned long)MicroSeconds() - store->sharedGlobals->lastMicroSeconds;
- theTime->value.hi = 0;
- theTime->scale = 1000000;
-
- return(noErr);
- }
-
- OSErr primeCallBack(SoundClockSharedGlobals sharedGlobals)
- {
- OSErr err;
- SndCommand cmd;
-
- cmd.cmd = bufferCmd;
- cmd.param1 = 0;
- cmd.param2 = (long) &sharedGlobals->header;
- err = SndDoImmediate(sharedGlobals->channel, &cmd);
- if (err) goto bail;
-
- cmd.cmd = callBackCmd;
- cmd.param1 = 0;
- cmd.param2 = (long)sharedGlobals;
- err = SndDoCommand(sharedGlobals->channel, &cmd, true); /* tell me when it's done */
- if (err) goto bail;
-
- bail:
- return err;
- }
-
- pascal void soundClockCompletionRoutine(SndChannelPtr chan, SndCommand *cmd)
- {
- SoundClockSharedGlobals sharedGlobals = (SoundClockSharedGlobals)cmd->param2;
-
- sharedGlobals->ticks += 1024;
- sharedGlobals->lastMicroSeconds = MicroSeconds();
-
- primeCallBack(sharedGlobals);
- }
-
-
- #if !INITBUILD
-
- Component RegisterSoundClockComponent(void);
- Component RegisterSoundClockComponent()
- {
- ComponentDescription someComponent;
- register Component systemClockComponent;
-
- someComponent.componentType = clockComponentType;
- someComponent.componentManufacturer = 'appl';
- someComponent.componentFlags = 3;
- someComponent.componentFlagsMask = 0;
- // someComponent.componentSubType = systemSoundClock;
-
- systemClockComponent = RegisterComponent(&someComponent, (void *)SoundClockComponent,1,0,0,0);
- SetDefaultComponent( systemClockComponent, 7 );
-
- return systemClockComponent;
- }
-
-
- #endif
-
-
-